<?php
/**
 * 【竞品信息反馈】接口控制器
 * 
 */
class competeAction extends apiAction {
	
	/**
	 * 竞品信息浏览
	 */
	public function index(){
		$page	= I('page', 1);	// 要显示第几页数据
		$num	= I('num', 20);	// 每页显示多少条数据
		
		$field = array(
			'c.id'	=> 'competeId',
			'c.price',
			'c.agents',
			'c.agentsPolicy',
			'c.storePolicy',
			'c.cmccPolicy',
			'b.name'=> 'brandName',
			'm.name'=> 'modelName',
		);
				
		$data = D(MODULE_NAME)->alias('c')->field($field)
				->join(C('DB_PREFIX')."model AS m ON m.id = c.modelId")
				->join(C('DB_PREFIX')."brand AS b ON b.id = m.brandId")
				->limit($num)->page($page)->order("createTime DESC")->select();
		
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 创建竞品信息反馈
	 */
	public function add(){
		$data['modelId'] = I('modelId');	//机型id
		$data['price'] = I('price');	//价格
		$data['agents'] = I('agents');	//代理商
		$data['agentsPolicy'] = I('agentsPolicy');	//代理商政策
		$data['storePolicy'] = I('storePolicy');	//门店政策
		$data['cmccPolicy'] = I('cmccPolicy');	//移动政策
		$data['createTime'] = time();
		$data['userId'] = $this->user['id'];
		
		$module = D(MODULE_NAME);
		if (!$module->create($data)){
			$this->ajaxReturn(300, $module->getError());
		}
		
		$compete = D(MODULE_NAME)->where(array('userId'=>$data['userId'], 'modelId'=>$data['modelId']))->find();
		
		if($compete){
			$res = D(MODULE_NAME)->where(array('id'=>$compete['id']))->save($data);
		}else{
			$res = D(MODULE_NAME)->add($data);
		}
		
		if($res){
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
	}

}
/* End of file competeAction.class.php */